﻿#If NETFX_CORE Then

Namespace Global.System.IO.IsolatedStorage

    Public Class IsolatedStorageFileStream
        Inherits Stream
        Implements IDisposable

        Private m_fileName As String
        Private m_fileMode As FileMode
        Private m_fileAccess As FileAccess
        Private m_fileStorage As IsolatedStorageFile
        Private m_baseStream As Stream

        Public ReadOnly Property BaseStream() As Stream
            Get
                Return Me.m_baseStream
            End Get
        End Property

        Public Overrides Property Position() As Long
            Get
                Return Me.BaseStream.Position
            End Get
            Set(value As Long)
                Me.BaseStream.Position = value
            End Set
        End Property

        Public Overrides ReadOnly Property CanRead As Boolean
            Get
                Return Me.BaseStream.CanRead
            End Get
        End Property

        Public Overrides ReadOnly Property CanSeek As Boolean
            Get
                Return Me.BaseStream.CanSeek
            End Get
        End Property

        Public Overrides ReadOnly Property CanWrite As Boolean
            Get
                Return Me.BaseStream.CanWrite
            End Get
        End Property

        Public Overrides ReadOnly Property Length As Long
            Get
                Return Me.BaseStream.Length
            End Get
        End Property

        Public Sub New(ByVal fileName As String, ByVal fileMode As FileMode, ByVal fileStorage As IsolatedStorageFile)
            Me.m_fileStorage = fileStorage
            Me.m_fileMode = fileMode
            Me.m_fileName = fileName
            Me.m_baseStream = fileStorage.OpenFile(fileName, fileMode)
        End Sub

        Public Sub New(ByVal fileName As String, ByVal fileMode As FileMode, ByVal fileAccess As FileAccess, ByVal fileStorage As IsolatedStorageFile)
            Me.m_fileName = fileName
            Me.m_fileMode = fileMode
            Me.m_fileAccess = fileAccess
            Me.m_fileStorage = fileStorage
            Me.m_baseStream = fileStorage.OpenFile(fileName, fileMode, fileAccess)
        End Sub

        Public Overrides Sub Flush()
            Me.BaseStream.Flush()
        End Sub

        Public Overrides Function Read(buffer() As Byte, offset As Integer, count As Integer) As Integer
            Return Me.BaseStream.Read(buffer, offset, count)
        End Function

        Public Overrides Function Seek(offset As Long, origin As SeekOrigin) As Long
            Return Me.BaseStream.Seek(offset, origin)
        End Function

        Public Overrides Sub SetLength(value As Long)
            Call Me.BaseStream.SetLength(value)
        End Sub

        Public Overrides Sub Write(buffer() As Byte, offset As Integer, count As Integer)
            Call Me.BaseStream.Write(buffer, offset, count)
        End Sub

#Region "IDisposable Support"
        Private disposedValue As Boolean ' To detect redundant calls

        ' IDisposable
        Protected Overloads Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: dispose managed state (managed objects).
                    Me.BaseStream.Flush()
                    Me.BaseStream.Dispose()
                End If

                ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
        End Sub

        ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
        'Protected Overrides Sub Finalize()
        '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        '    Dispose(False)
        '    MyBase.Finalize()
        'End Sub

        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Overloads Sub Dispose()
            ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class

End Namespace

#End If